package com.basic.library.binding.viewadapter.view;

import android.annotation.SuppressLint;
import android.view.View;
import android.widget.TextView;

import androidx.databinding.BindingAdapter;

import com.basic.library.binding.command.BindingCommand;
import com.basic.library.binding.command.BindingCommandAbstract;
import com.basic.library.utils.ScreenUtils;
import com.jakewharton.rxbinding3.view.RxView;

import java.util.concurrent.TimeUnit;

import io.reactivex.functions.Consumer;

/**
 * Created by goldze on 2017/6/16.
 */

@SuppressLint("CheckResult")
public class ViewAdapter {
    //防重复点击间隔(秒)
    private static final int CLICK_INTERVAL = 680;

    /**
     * requireAll 是意思是是否需要绑定全部参数, false为否
     * View的onClick事件绑定
     * onClickCommand 绑定的命令,
     * isNoThrottleFirst 是否开启点击防抖(默认开启)
     */
    @BindingAdapter(value = {"onClickCommand", "isNoThrottleFirst", "clickInterval"}, requireAll = false)
    public static <T> void onClickCommand(View view,
                                          final BindingCommandAbstract<T> clickCommand,
                                          final boolean isNoThrottleFirst,
                                          final int clickInterval) {
        if (clickCommand == null) return;
        int diff = clickInterval > 0 ? clickInterval : CLICK_INTERVAL;
        if (isNoThrottleFirst) {
            RxView.clicks(view)
                    .subscribe(new Consumer<Object>() {
                        @Override
                        public void accept(Object object) throws Exception {
                            clickCommand.execute();
                            if (clickCommand instanceof BindingCommand || clickCommand.mType == Integer.class) {
                                Integer id = view.getId();
                                clickCommand.execute((T) id);
                            } else if (clickCommand.mType == String.class) {
                                if (view instanceof TextView)
                                    clickCommand.execute((T) ((TextView) view).getText().toString());
                            }
                        }
                    });
        } else {
            RxView.clicks(view)
                    .throttleFirst(diff, TimeUnit.MILLISECONDS)//diff秒钟内只允许点击1次
                    .subscribe(new Consumer<Object>() {
                        @Override
                        public void accept(Object object) throws Exception {
                            clickCommand.execute();
                            if (clickCommand instanceof BindingCommand || clickCommand.mType == Integer.class) {
                                Integer id = view.getId();
                                clickCommand.execute((T) id);
                            } else if (clickCommand.mType == String.class) {
                                if (view instanceof TextView)
                                    clickCommand.execute((T) ((TextView) view).getText().toString());
                            }
                        }
                    });
        }
    }

    /**
     * view的onLongClick事件绑定
     */
    @BindingAdapter(value = {"onLongClickCommand"}, requireAll = false)
    public static void onLongClickCommand(View view, final BindingCommand clickCommand) {
        RxView.longClicks(view)
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object object) throws Exception {
                        if (clickCommand != null) {
                            clickCommand.execute();
                        }
                    }
                });
    }

    /**
     * 回调控件本身
     *
     * @param currentView
     * @param bindingCommand
     */
    @BindingAdapter(value = {"currentView"}, requireAll = false)
    public static void replyCurrentView(View currentView, BindingCommand bindingCommand) {
        if (bindingCommand != null) {
            bindingCommand.execute(currentView);
        }
    }

    /**
     * view是否需要获取焦点
     */
    @BindingAdapter({"requestFocus"})
    public static void requestFocusCommand(View view, final Boolean needRequestFocus) {
        if (needRequestFocus) {
            view.setFocusableInTouchMode(true);
            view.requestFocus();
        } else {
            view.clearFocus();
        }
    }

    /**
     * view的焦点发生变化的事件绑定
     */
    @BindingAdapter({"onFocusChangeCommand"})
    public static void onFocusChangeCommand(View view,
                                            final BindingCommand<Boolean> onFocusChangeCommand) {
        view.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (onFocusChangeCommand != null) {
                    onFocusChangeCommand.execute(hasFocus);
                }
            }
        });
    }

//    @BindingAdapter({"onTouchCommand"})
//    public static void onTouchCommand(View view, final ResponseCommand<MotionEvent, Boolean> onTouchCommand) {
//        view.setOnTouchListener(new View.OnTouchListener() {
//            @Override
//            public boolean onTouch(View v, MotionEvent event) {
//                if (onTouchCommand != null) {
//                    return onTouchCommand.execute(event);
//                }
//                return false;
//            }
//        });
//    }

    @BindingAdapter({"onTouchListener"})
    public static void onTouchListener(View view, final View.OnTouchListener onTouchListener) {
        view.setOnTouchListener(onTouchListener);
    }

    /**
     * padding状态栏
     *
     * @param view
     * @param fitSystemPadding
     */
    @BindingAdapter({"fitSystemPadding"})
    public static void fitSystemPadding(View view, boolean fitSystemPadding) {
        if (fitSystemPadding) {
            int paddingTop = view.getPaddingTop() + ScreenUtils.getStatusHeight(view.getContext());
            int paddingLeft = Math.max(view.getPaddingStart(), view.getPaddingLeft());
            int paddingRight = Math.max(view.getPaddingEnd(), view.getPaddingRight());
            view.setPadding(paddingLeft, paddingTop, paddingRight, view.getPaddingBottom());
        }
    }
}
